#ifndef GAMEKIT_COMPONENTS_SCRIPTINGCOMPONENT_H
#define GAMEKIT_COMPONENTS_SCRIPTINGCOMPONENT_H

#include <string>
#include <map>
#include <vector>

#include "gamekit/gamekit.h"
#include "gamekit/Component.h"

#include "gamekit/common/Delegate.h"

#include "gamekit/scripting/Wrappers.h"
#include "gamekit/scripting/Arguments.h"
#include "gamekit/scripting/Function.h"
#include "gamekit/scripting/Result.h"
#include "gamekit/scripting/Module.h"
#include "gamekit/scripting/ModuleBuilder.h"

struct lua_State;

#include "gamekit/gamekit.h"

using namespace gamekit::scripting;

namespace gamekit
{
	EXPORT_STL_MAP(GK_API, std::string, Module*)

	class GK_API ScriptingComponent : public Component
	{
		typedef std::map<std::string, Module*> ModuleMap;
		typedef std::map<std::string, Module*>::const_iterator ModuleMapIter;
		typedef int (*LuaFunction) (lua_State*);

	public:
		ScriptingComponent(Scene* scene);
		~ScriptingComponent();

		void execFile(const std::string& file);
		void exec(const std::string& script);

		void onInit(void);

		ModuleBuilder function(const std::string& name, LuaFunction func);
		ModuleBuilder module(const std::string name);

	private:
		void closeLua(void);
		void openLua(void);
		void printError(void);

		Module* getModule(const std::string& name);
		Module* createModule(const std::string& name);

		void registerGlobalModule(Module* module);
		void registerModule(Module* module);

	private:
		lua_State* m_state;
		ModuleMap m_modules;
		bool m_initialized;
	};
}

#endif
